class BaseTerm {
  cn_short?: string
  cn_full?: string
  en_short?: string
  en_full?: string
  desc?: string

  constructor({}) {
    //todo
  }
}

export class Term extends BaseTerm {
  constructor(args: BaseTerm) {
    super(args)
    Object.assign(this, args)
  }

  static _keyword = ''
  score = 0


  match(keyword: string): boolean {
    this.score = 0;
    Term._keyword = keyword.toLowerCase();
    // 搜索评分
    if(
      this.en_full == Term._keyword ||
      this.cn_full == Term._keyword ||
      this.en_short == Term._keyword ||
      this.cn_short == Term._keyword ||
      this.desc == Term._keyword
    ) {
      this.score = 100;
      return true
    }
    if(
      this.en_full && this.en_full.toLowerCase() == Term._keyword.toLowerCase() ||
      this.cn_full && this.cn_full.toLowerCase() == Term._keyword.toLowerCase() ||
      this.en_short && this.en_short.toLowerCase() == Term._keyword.toLowerCase() ||
      this.cn_short && this.cn_short.toLowerCase() == Term._keyword.toLowerCase() ||
      this.desc && this.desc.toLowerCase() == Term._keyword.toLowerCase()
    ) {
      this.score += 50;
      return true
    }
    if(this.en_short.toLowerCase().includes(Term._keyword)){
      this.score += 10;
    }
    if((this.cn_short && this.cn_short.toLowerCase().includes(Term._keyword))) {
      this.score += 10;
    }
    if(this.en_short && Term._keyword.includes(this.en_short.toLowerCase())){
      this.score += 7;
    }
    if(this.cn_short && Term._keyword.includes(this.cn_short.toLowerCase())) {
      this.score += 7;
    }
    if((this.en_full && this.en_full.toLowerCase().includes(Term._keyword))) {
      this.score += 3;
    }
    if((this.cn_full && this.cn_full.toLowerCase().includes(Term._keyword))) {
      this.score += 3;
    }
    if((this.desc && this.desc.toLowerCase().includes(Term._keyword))) {
      this.score += 1;
    }
    return this.score > 0
  }

  getTitle(): string {
    let title = this.en_short
    if (this.cn_short) {
      title += "(" + this.cn_short + ")"
    }
    let index = -1;
    if ((index = title.toLowerCase().indexOf(Term._keyword)) != -1) {
      title = title.substring(0, index)
        + '<cusmark class="cus-mark">' + title.substring(index, index + Term._keyword.length)
        + '</cusmark>' + title.substring(index + Term._keyword.length)
    }else if ((Term._keyword.indexOf(this.en_short.toLowerCase())) != -1) {
      title = '<cusmark2 class="cus-mark2">' + title + '</cusmark2>'
    }
    return title
  }

  getBody() {
    let body = null
    if (this.cn_full) {
      body = this.cn_full
    }
    if (this.en_full) {
      body += "(" + this.en_full + ")"
    }
    if (this.desc) {
      body += ": " + this.desc
    }
    let index = -1;
    if (body && (index = body.toLowerCase().indexOf(Term._keyword)) != -1) {
      body = body.substring(0, index)
        + '<cusmark class="cus-mark">' + body.substring(index, index + Term._keyword.length)
        + '</cusmark>' + body.substring(index + Term._keyword.length)
    }
    return body || '无'
  }
}

